package com.onlineMIS.action.headQ.supplier.supplierMgmt;

import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.onlineMIS.ORM.DAO.Response;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.HeadQTagDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.supplier.supplierMgmt.HeadQSupplierDaoImpl;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTag;
import com.onlineMIS.ORM.entity.headQ.custMgmt.HeadQCustVO;
import com.onlineMIS.ORM.entity.headQ.supplier.supplierMgmt.HeadQSupplier;
import com.onlineMIS.ORM.entity.headQ.supplier.supplierMgmt.HeadQSupplierVO;
import com.onlineMIS.common.loggerLocal;
import com.opensymphony.xwork2.ActionContext;

public class HeadQSupplierMgmtJSPAction extends HeadQSupplierMgmtAction {

	@Autowired
	private HeadQSupplierDaoImpl headQSupplierDaoImpl;

	@Autowired
	private HeadQTagDaoImpl headQTagDaoImpl;

	/**
	 * 
	 */
	private static final long serialVersionUID = -4965927215338212593L;
	private final String templateHeadSupplier = "HeadSupplierTemplate.xls";

	/**
	 * 进入list cust 页面
	 * 
	 * @return
	 */
	public String preSupplier() {
		loggerLocal.info("HeadQSupplierMgmtJSPAction.preSupplier");
		// 获取所有标签
		List<HeadQTag> tags = headQTagDaoImpl.getAllSupplierTags();
		uiBean.setTags(tags);

		return "listSupplier";

	}

	/**
	 * 准备添加或者修改某个客户
	 * 
	 * @param headQCust
	 * @return
	 */
	public String preEditSupplier() {
		loggerLocal.info("HeadQSupplierMgmtJSPAction.preEditSupplier");

		Response response = headQSupplierService.preEditSupplier(formBean.getSupplier());
		if (response.isSuccess()) {
			HeadQSupplier supplier = (HeadQSupplier) response.getReturnValue();
			formBean.setSupplier(supplier);
		} else
			addActionError(response.getMessage());

		return "editSupplier";
	}

	/**
	 * 下载供应商信息
	 * @return
	 */
	public String download() {
		loggerLocal.info(this.getClass().getName() + ".downloadAcctFlow");
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		String contextPath = request.getRealPath("/");
		Response response = new Response();
		try {
			response = headQSupplierService.generateHeadSupplierExcel(contextPath + "WEB-INF\\template\\" + templateHeadSupplier, formBean);
		} catch (Exception e) {
			response.setReturnCode(Response.FAIL);
			response.setMessage(e.getMessage());
		}
		if (response.getReturnCode() == Response.SUCCESS) {
			InputStream excelStream = (InputStream) response.getReturnValue();
			formBean.setFileStream(excelStream);
			formBean.setFileName(templateHeadSupplier);
			return "download";
		} else
			return ERROR;
	}

	/**
	 * 供应商页面
	 * 
	 * @return
	 */
	public String goHeadSupplierTag() {
		loggerLocal.info("HeadQCustMgmtJSPAction - goHeadCustTag");

		// 获取所有标签
		List<HeadQTag> tags = headQTagDaoImpl.getAllSupplierTags();
		uiBean.setTags(tags);

		addActionMessage("查找成功！");
		return "listSupplier";
	}



	/**
	 * 根据标签查找供应商
	 * 
	 * @return
	 */
	public String findCustByTagId() {
		loggerLocal.info("HeadQSalesJSPAction.findCustByTagId");

		// 获取所有标签
		List<HeadQTag> tags = headQTagDaoImpl.getAllSupplierTags();
		uiBean.setTags(tags);

		return "headCustTag";
	}

}
